﻿using Google.Protobuf.Collections;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Qdrant.Client.Grpc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace QdrantConnector
{

    /**
     * 有bug尚未修复，暂时支持。
     * */
    [Icon("pack://application:,,,/QdrantConnector;component/Resources/Icon.png")]
    [Designer("QdrantConnector.Designer.QdrantConnectorServerCommandDesigner, QdrantConnector")]
    [Category("Qdrant向量数据库")]
    [OrderWeight(310)]
    internal abstract class BatchRemoveFromCollection : BaseQdrantOp
    {
        [OrderWeight(120)]
        [DisplayName("点击设置查询条件（filter）")]
        [Description("查询条件为AND关系，即所有条件全部满足才会被删除。")]
        [ListProperty]

        public List<QueryConditionObject> QueryConditions { get; set; }

        public override async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {

            var client = await getQrantClient(dataContext);

            var collectionName = await getCollectionName(dataContext);

            dataContext.Log.AppendLine($"开始从Qdrant集合【{collectionName}】中批量删除符合条件的向量...");
            var tick = DateTime.Now;

            await client.DeleteAsync(collectionName, await QdrantFilterBuilder.BuildFilterAsync(dataContext, QueryConditions));

            dataContext.Log.AppendLine($"Qdrant批量删除集合【{collectionName}】中符合条件的向量成功，耗时：{(DateTime.Now - tick).TotalMilliseconds}ms");

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "Qdrant批量删除向量";
        }

    }
}
